package com.dragon.myapplication.padings;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.paging.LivePagedListBuilder;
import androidx.paging.PagedList;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.room.util.FileUtil;

import android.os.Bundle;
import android.os.FileUtils;
import android.util.Log;
import android.widget.Button;

import com.dragon.myapplication.R;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;

public class PadingActivity extends AppCompatActivity {
    private Button insert,clear;
    private StudentsDataBase dataBase;
    private StudentDao studentDao;
    private PageAdapter myAdapter;
    private RecyclerView recyclerView;
    private LiveData<PagedList<Student>> allStudent;
    private final CompositeDisposable mDisposable = new CompositeDisposable();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pading);
        insert = findViewById(R.id.insert);
        clear = findViewById(R.id.clear);
        recyclerView = findViewById(R.id.recycle);
        myAdapter = new PageAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        recyclerView.setAdapter(myAdapter);

        dataBase = StudentsDataBase.getInstance(this);

        studentDao = dataBase.getStudentDao();

        //分页数量  以及获取数据
        allStudent = new LivePagedListBuilder<>(studentDao.getAllStudent(),10).build();

        //LiveData的观察
        allStudent.observe(this, new Observer<PagedList<Student>>() {
            @Override
            public void onChanged(PagedList<Student> students) {
                myAdapter.submitList(students);
                students.addWeakCallback(null, new PagedList.Callback() {
                    @Override
                    public void onChanged(int position, int count) {
                        //从数据源加载更多的时候调用
                    }

                    @Override
                    public void onInserted(int position, int count) {

                    }

                    @Override
                    public void onRemoved(int position, int count) {

                    }
                });
            }
        });

        insert = findViewById(R.id.insert);
        clear = findViewById(R.id.clear);

        insert.setOnClickListener( v -> {
            insert.setClickable(false);
            Student[] students = new Student[100];
            for (int i = 0; i < 100; i++) {
                Student student = new Student();
                student.setNumber(i);
                students[i] = student;
            }
            mDisposable.add(studentDao.insertStudent(students).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(
                            () ->{insert.setClickable(true);},throwable -> Log.d("TAG", "onCreate: "+ throwable)
                    )
            );
        });

        clear.setOnClickListener(v -> {
            mDisposable.add(studentDao.deleteAllStudents().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                    .subscribe()
            );
        });


    }

    @Override
    protected void onStop() {
        super.onStop();
        mDisposable.clear();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mDisposable.dispose();
    }
}